﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainClient
{
    public partial class frmBackupDB : Form
    {
        public frmBackupDB()
        {
            InitializeComponent();
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            Console.WriteLine(""+Application.StartupPath.ToString());
            this.sfdBackupDB.InitialDirectory = Application.StartupPath.ToString() + "\\Backup";
            this.sfdBackupDB.Filter = "数据库备份文件(*.bak)|*.bak";
            this.sfdBackupDB.Title = "保存备份文件";
            this.sfdBackupDB.CreatePrompt = true;
            if (this.sfdBackupDB.ShowDialog() == DialogResult.OK)
            {
                this.txtFilePath.Text = this.sfdBackupDB.FileName;
            }
        }

        private void btnBackupDB_Click(object sender, EventArgs e)
        {
            try
            {
                Console.WriteLine("备份至目标文件：" + this.txtFilePath.Text.Trim());
                if (!System.IO.File.Exists(this.txtFilePath.Text.Trim()))
                {
                    System.IO.File.Create(this.txtFilePath.Text.Trim()).Close();
                }
                DataAccess.DBDAO dbDAO = new DataAccess.DBDAO();
                dbDAO.BackupDB(this.txtFilePath.Text.Trim());
                MessageBox.Show("备份成功", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("备份失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
